package 集合.二叉树;

import java.util.Comparator;
import java.util.TreeSet;

public class treeSetDemo2 {
    public static void main(String[] args) {
        /*
        需求：建立五个学生对象
        属性：姓名，年龄，语文成绩，数学成绩，英语成绩
        按照总分从高到低输出到控制台
        如果总分一样，按照语文成绩
        如果语文一样，按照数学成绩
        如果数学一样，按照英语成绩
        如果英语一样，按照年龄
        如果年龄一样，按照姓名首字母排列
        如果都一样，则认为是同一个学生。不存
         */
//        创建学生对象
        student1 s1 = new student1("zhangsan", 18, 90, 80, 70);
        student1 s2 = new student1("lisi", 17, 80, 90, 70);
        student1 s3 = new student1("wangwu", 18, 90, 70, 80);
        student1 s4 = new student1("zhaoliu", 19, 70, 80, 90);
        student1 s5 = new student1("liuba", 19, 70, 90, 80);
//        创建集合存储学生对象
//        比较器比较
//        o1表示当前要添加元素
//        o2表示已经存在的元素
        TreeSet<student1> ts=new TreeSet<>();
        /*
        TreeSet<student1> ts=new TreeSet<>(new Comparator<student1>() {
            @Override
            public int compare(student1 o1, student1 o2) {
//                比较总成绩
                int temp= o1.getChineseScore()+ o1.getEnglishScore()+o1.getMathScore()-
                        o2.getChineseScore()-o2.getMathScore()-o2.getEnglishScore();
//               比较语文成绩
                temp=temp==0?o1.getChineseScore()-o2.getChineseScore():temp;
//                比较数学成绩
                temp=temp==0?o1.getMathScore()-o2.getMathScore():temp;
//                比较英语成绩
                temp=temp==0?o1.getEnglishScore()-o2.getEnglishScore():temp;
//                比较年龄
                temp=temp==0?o1.getAge()-o2.getAge():temp;
//                比较姓名的首字母
                temp=temp==0?o1.getName().compareTo(o2.getName()):temp;
                return temp;
            }
        });

         */
            ts.add(s1);
            ts.add(s2);
            ts.add(s3);
            ts.add(s4);
            ts.add(s5);
        System.out.println(ts);
     /*   for (student1 t:ts){
            System.out.println(ts);
        }*/
    }
}
class student1 implements Comparable<student1>{
    private String name;
    private int age;
    private int chineseScore;
    private int MathScore;
    private int EnglishScore;

    public student1() {
    }

    public student1(String name, int age, int chineseScore, int MathScore, int EnglishScore) {
        this.name = name;
        this.age = age;
        this.chineseScore = chineseScore;
        this.MathScore = MathScore;
        this.EnglishScore = EnglishScore;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return chineseScore
     */
    public int getChineseScore() {
        return chineseScore;
    }

    /**
     * 设置
     * @param chineseScore
     */
    public void setChineseScore(int chineseScore) {
        this.chineseScore = chineseScore;
    }

    /**
     * 获取
     * @return MathScore
     */
    public int getMathScore() {
        return MathScore;
    }

    /**
     * 设置
     * @param MathScore
     */
    public void setMathScore(int MathScore) {
        this.MathScore = MathScore;
    }

    /**
     * 获取
     * @return EnglishScore
     */
    public int getEnglishScore() {
        return EnglishScore;
    }

    /**
     * 设置
     * @param EnglishScore
     */
    public void setEnglishScore(int EnglishScore) {
        this.EnglishScore = EnglishScore;
    }

    public String toString() {
        return "student1{name = " + name + ", age = " + age + ", chineseScore = " + chineseScore + ", MathScore = " + MathScore + ", EnglishScore = " + EnglishScore + "}";
    }

    @Override
    public int compareTo(student1 o) {
        //                比较总成绩
        int temp= this.getChineseScore()+ this.getEnglishScore()+this.getMathScore()-
                this.getChineseScore()-o.getMathScore()-o.getEnglishScore();
//               比较语文成绩
        temp=temp==0?this.getChineseScore()-o.getChineseScore():temp;
//                比较数学成绩
        temp=temp==0?this.getMathScore()-o.getMathScore():temp;
//                比较英语成绩
        temp=temp==0?this.getEnglishScore()-o.getEnglishScore():temp;
//                比较年龄
        temp=temp==0?this.getAge()-o.getAge():temp;
//                比较姓名的首字母
        temp=temp==0?this.getName().compareTo(o.getName()):temp;
        return temp;
    }
}
